﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Zero.UControl.Component
{
    /// <summary>
    /// Pipeline.xaml 的交互逻辑
    /// </summary>
    public partial class Pipeline : UserControl
    {
        public Pipeline()
        {
            InitializeComponent();
            SizeChanged += Pipeline_SizeChanged;
        }

        private void Pipeline_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.InvalidateVisual();
        }

        public bool Flow
        {
            get { return (bool)GetValue(FlowProperty); }
            set { SetValue(FlowProperty, value); }
        }

        public static readonly DependencyProperty FlowProperty =
            DependencyProperty.Register("Flow", typeof(bool), typeof(Pipeline), new PropertyMetadata(false,new PropertyChangedCallback(OnPropertyChanged)));



        public string Direction
        {
            get { return (string)GetValue(DirectionProperty); }
            set { SetValue(DirectionProperty, value); }
        }

        public static readonly DependencyProperty DirectionProperty =
            DependencyProperty.Register("Direction", typeof(string), typeof(Pipeline), new PropertyMetadata("Horizontal", new PropertyChangedCallback(OnPropertyChanged)));



        public string BeginDir
        {
            get { return (string)GetValue(BeginDirProperty); }
            set { SetValue(BeginDirProperty, value); }
        }

        public static readonly DependencyProperty BeginDirProperty =
            DependencyProperty.Register("BeginDir", typeof(string), typeof(Pipeline), new PropertyMetadata("",new PropertyChangedCallback(OnPropertyChanged)));



        public string EndDir
        {
            get { return (string)GetValue(EndDirProperty); }
            set { SetValue(EndDirProperty, value); }
        }

        public static readonly DependencyProperty EndDirProperty =
            DependencyProperty.Register("EndDir", typeof(string), typeof(Pipeline), new PropertyMetadata("", new PropertyChangedCallback(OnPropertyChanged)));


        public FlowDirections FlowDir
        {
            get { return (FlowDirections)GetValue(FlowDirProperty); }
            set { SetValue(FlowDirProperty, value); }
        }

        public static readonly DependencyProperty FlowDirProperty =
            DependencyProperty.Register("FlowDir", typeof(FlowDirections), typeof(Pipeline), new PropertyMetadata(FlowDirections.FORWARD, OnPropertyChanged));


        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as Pipeline).InvalidateVisual();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);
            double len1 = Math.Max(ActualWidth, ActualHeight);
            double len2 = Math.Min(ActualWidth, ActualHeight);
            double Radius = len2;

            if (len1 - 2 * len2 < 0)
            {
                len1 = 2 * len2 + 10;
            }
            DrawPipe(drawingContext, Radius,len1,len2); 
        }

        /// <summary>
        /// 管道绘制
        /// </summary>
        /// <param name="drawingContext"></param>
        /// <param name="Radius"></param>
        /// <param name="len1">长轴</param>
        /// <param name="len2">短轴</param>
        private void DrawPipe(DrawingContext drawingContext, double Radius,double len1,double len2)
        {
            // 径向发散
            RadialGradientBrush radBrush = new RadialGradientBrush();
            radBrush.GradientOrigin = new Point(0.5, 0.5);
            radBrush.GradientStops.Add(new GradientStop(Colors.Gray, 0));
            radBrush.GradientStops.Add(new GradientStop(Colors.White, 0.5));
            radBrush.GradientStops.Add(new GradientStop(Colors.Gray, 1));

            // 线性发散
            LinearGradientBrush linearBrush = new LinearGradientBrush();
            linearBrush.GradientStops.Add(new GradientStop(Colors.Gray, 0));
            linearBrush.GradientStops.Add(new GradientStop(Colors.White, 0.5));
            linearBrush.GradientStops.Add(new GradientStop(Colors.Gray, 1));

            // 路径图形
            PathFigure pathFigure = new PathFigure();

            if (Direction == "Horizontal")
            {
                linearBrush.StartPoint = new Point(0, 0);
                linearBrush.EndPoint = new Point(0, 1);
                // 两个端点都是水平的
                if(string.IsNullOrEmpty(BeginDir) && string.IsNullOrEmpty(EndDir))
                {
                    drawingContext.DrawRectangle(linearBrush, null, new Rect(0, 0, len1, Radius));

                    Point p0 = new Point(0, Radius / 2);
                    Point p1 = new Point(len1, Radius / 2);
                    pathFigure.StartPoint = p0;
                    LineSegment lineSegment = new LineSegment(p1, true);
                    pathFigure.Segments.Add(lineSegment);
                }
                // 起点水平，结束端点带方向
                else if (string.IsNullOrEmpty(BeginDir))
                {
                    // 绘制中间矩形
                    drawingContext.DrawRectangle(linearBrush, null, new Rect(0, 0, len1, Radius));
                    Point p0;
                    Point p1;
                    Point p2;
                    Point p3;
                    if (EndDir == "Top")
                    {
                        // 绘制二区弧形
                        EllipseGeometry eGeometry2 = new EllipseGeometry(new Point(len1 - Radius, 0), Radius, Radius);
                        RectangleGeometry rGeometry2 = new RectangleGeometry(new Rect(len1 - Radius, 0, Radius, Radius));
                        CombinedGeometry comb2 = new CombinedGeometry(eGeometry2, rGeometry2);
                        comb2.GeometryCombineMode = GeometryCombineMode.Intersect;

                        drawingContext.PushClip(comb2);
                        drawingContext.DrawEllipse(radBrush, null, new Point(len1 - Radius, 0), Radius, Radius);
                        drawingContext.Pop();

                        // 蚂蚁线坐标点
                        p0 = new Point(0, Radius / 2);
                        p1 = new Point(len1 - Radius, Radius / 2);
                        p2 = new Point(len1 - Radius / 2, Radius / 2);
                        p3 = new Point(len1 - Radius / 2, 0);

                        pathFigure.StartPoint = p0;
                        LineSegment line = new LineSegment(p1, true);
                        BezierSegment bezier = new BezierSegment(p1, p2, p3, true);

                        pathFigure.Segments.Add(line);
                        pathFigure.Segments.Add(bezier);
                    }
                    else
                    {
                        // 绘制二区弧形
                        EllipseGeometry eGeometry2 = new EllipseGeometry(new Point(len1 - Radius, Radius), Radius, Radius);
                        RectangleGeometry rGeometry2 = new RectangleGeometry(new Rect(len1 - Radius, 0, Radius, Radius));
                        CombinedGeometry comb2 = new CombinedGeometry(eGeometry2, rGeometry2);
                        comb2.GeometryCombineMode = GeometryCombineMode.Intersect;

                        drawingContext.PushClip(comb2);
                        drawingContext.DrawEllipse(radBrush, null, new Point(len1 - Radius, Radius), Radius, Radius);
                        drawingContext.Pop();

                        // 蚂蚁线坐标点
                        p0 = new Point(0, Radius / 2);
                        p1 = new Point(len1 - Radius, Radius / 2);
                        p2 = new Point(len1 - Radius / 2, Radius / 2);
                        p3 = new Point(len1 - Radius / 2, Radius);

                        pathFigure.StartPoint = p0;
                        LineSegment line = new LineSegment(p1, true);
                        BezierSegment bezier = new BezierSegment(p1, p2, p3, true);

                        pathFigure.Segments.Add(line);
                        pathFigure.Segments.Add(bezier);
                    }
                }
                // 起点带方向，结束端点水平
                else if (string.IsNullOrEmpty(EndDir))
                {
                    Point p0;
                    Point p1;
                    Point p2;
                    Point p3;
                    if (BeginDir == "Top")
                    {
                        // 绘制一区弧形
                        EllipseGeometry eGeometry = new EllipseGeometry(new Point(Radius, 0), Radius, Radius);
                        RectangleGeometry rGeometry = new RectangleGeometry(new Rect(x: 0, 0, Radius, Radius));
                        CombinedGeometry comb = new CombinedGeometry(eGeometry, rGeometry);
                        comb.GeometryCombineMode = GeometryCombineMode.Intersect;

                        drawingContext.PushClip(comb);
                        drawingContext.DrawEllipse(radBrush, null, new Point(Radius, 0), Radius, Radius);
                        drawingContext.Pop();

                        // 蚂蚁线坐标点
                        p0 = new Point(Radius / 2, 0);
                        p1 = new Point(Radius / 2, Radius / 2);
                        p2 = new Point(Radius, Radius / 2);
                        p3 = new Point(len1, Radius / 2);

                        pathFigure.StartPoint = p0;
                        LineSegment line = new LineSegment(p1, true);
                        BezierSegment bezier = new BezierSegment(p1, p2, p3, true);

                        pathFigure.Segments.Add(line);
                        pathFigure.Segments.Add(bezier);
                    }
                    else
                    {
                        // 绘制一区弧形
                        EllipseGeometry eGeometry = new EllipseGeometry(new Point(Radius, Radius), Radius, Radius);
                        RectangleGeometry rGeometry = new RectangleGeometry(new Rect(x: 0, 0, Radius, Radius));
                        CombinedGeometry comb = new CombinedGeometry(eGeometry, rGeometry);
                        comb.GeometryCombineMode = GeometryCombineMode.Intersect;

                        drawingContext.PushClip(comb);
                        drawingContext.DrawEllipse(radBrush, null, new Point(Radius, Radius), Radius, Radius);
                        drawingContext.Pop();

                        // 蚂蚁线坐标点
                        p0 = new Point(Radius / 2, 0);
                        p1 = new Point(Radius / 2, Radius / 2);
                        p2 = new Point(Radius, Radius / 2);
                        p3 = new Point(len1, Radius / 2);

                        pathFigure.StartPoint = p0;
                        LineSegment line = new LineSegment(p1, true);
                        BezierSegment bezier = new BezierSegment(p1, p2, p3, true);

                        pathFigure.Segments.Add(line);
                        pathFigure.Segments.Add(bezier);
                    }
                   
                    // 绘制中间矩形
                    drawingContext.DrawRectangle(linearBrush, null, new Rect(0, 0, len1, Radius));

                }
                else
                {
                    // 蚂蚁线坐标点
                    Point p0;
                    Point p1;
                    Point p2;
                    Point p3;
                    Point p4;
                    Point p5;

                    // 绘制一区弧形
                    if (BeginDir == "Top")
                    {
                        EllipseGeometry eGeometry = new EllipseGeometry(new Point(Radius, 0), Radius, Radius);
                        RectangleGeometry rGeometry = new RectangleGeometry(new Rect(x: 0, 0, Radius, Radius));
                        CombinedGeometry comb = new CombinedGeometry(eGeometry, rGeometry);
                        comb.GeometryCombineMode = GeometryCombineMode.Intersect;

                        drawingContext.PushClip(comb);
                        drawingContext.DrawEllipse(radBrush, null, new Point(Radius, 0), Radius, Radius);
                        drawingContext.Pop();

                        p0 = new Point(Radius / 2, 0);
                        p1 = new Point(Radius / 2, Radius / 2);
                        p2 = new Point(Radius, Radius / 2);
                    }
                    else
                    {
                        EllipseGeometry eGeometry = new EllipseGeometry(new Point(Radius, Radius), Radius, Radius);
                        RectangleGeometry rGeometry = new RectangleGeometry(new Rect(x: 0, 0, Radius, Radius));
                        CombinedGeometry comb = new CombinedGeometry(eGeometry, rGeometry);
                        comb.GeometryCombineMode = GeometryCombineMode.Intersect;

                        drawingContext.PushClip(comb);
                        drawingContext.DrawEllipse(radBrush, null, new Point(Radius, Radius), Radius, Radius);
                        drawingContext.Pop();

                        p0 = new Point(Radius / 2, Radius);
                        p1 = new Point(Radius / 2, Radius / 2);
                        p2 = new Point(Radius, Radius / 2);
                    }
                    
                    // 绘制中间矩形
                    drawingContext.DrawRectangle(linearBrush, null, new Rect(Radius - 1, 0, len1 - 2 * Radius + 2, len2));

                    // 绘制二区弧形
                    if (EndDir == "Top")
                    {
                        EllipseGeometry eGeometry2 = new EllipseGeometry(new Point(len1 - Radius, 0), Radius, Radius);
                        RectangleGeometry rGeometry2 = new RectangleGeometry(new Rect(len1 - Radius, 0, Radius, Radius));
                        CombinedGeometry comb2 = new CombinedGeometry(eGeometry2, rGeometry2);
                        comb2.GeometryCombineMode = GeometryCombineMode.Intersect;

                        drawingContext.PushClip(comb2);
                        drawingContext.DrawEllipse(radBrush, null, new Point(len1 - Radius, 0), Radius, Radius);
                        drawingContext.Pop();

                        // 蚂蚁线坐标点
                        p3 = new Point(len1 - Radius, Radius / 2);
                        p4 = new Point(len1 - Radius / 2, Radius / 2);
                        p5 = new Point(len1 - Radius / 2, 0);
                    }
                    else
                    {
                        EllipseGeometry eGeometry2 = new EllipseGeometry(new Point(len1 - Radius, Radius), Radius, Radius);
                        RectangleGeometry rGeometry2 = new RectangleGeometry(new Rect(len1 - Radius, 0, Radius, Radius));
                        CombinedGeometry comb2 = new CombinedGeometry(eGeometry2, rGeometry2);
                        comb2.GeometryCombineMode = GeometryCombineMode.Intersect;

                        drawingContext.PushClip(comb2);
                        drawingContext.DrawEllipse(radBrush, null, new Point(len1 - Radius, Radius), Radius, Radius);
                        drawingContext.Pop();

                        // 蚂蚁线坐标点
                        p3 = new Point(len1 - Radius, Radius / 2);
                        p4 = new Point(len1 - Radius / 2, Radius / 2);
                        p5 = new Point(len1 - Radius / 2, Radius);
                    }

                    pathFigure.StartPoint = p0;
                    BezierSegment bezier1 = new BezierSegment(p0, p1, p2,  true);
                    LineSegment line = new LineSegment(p3, true);
                    BezierSegment bezier2 = new BezierSegment(p3, p4, p5, true);

                    pathFigure.Segments.Add(bezier1);
                    pathFigure.Segments.Add(line);
                    pathFigure.Segments.Add(bezier2);
                }
                
            }
            else // 垂直方向
            {
                linearBrush.StartPoint = new Point(0, 0);
                linearBrush.EndPoint = new Point(1, 0);
                // 两个端点都是垂直的
                if (string.IsNullOrEmpty(BeginDir) && string.IsNullOrEmpty(EndDir))
                {
                    drawingContext.DrawRectangle(linearBrush, null, new Rect(0, 0,len2, len1));

                    Point p0 = new Point(Radius / 2, 0);
                    Point p1 = new Point(Radius / 2, len1);
                    pathFigure.StartPoint = p0;
                    LineSegment lineSegment = new LineSegment(p1, true);
                    pathFigure.Segments.Add(lineSegment);
                }
                // 起点垂直，结束端点带方向
                else if (string.IsNullOrEmpty(BeginDir))
                {
                    Point p0;
                    Point p1;
                    Point p2;
                    Point p3;
                    // 绘制二区弧形
                    if (EndDir == "Left")
                    {
                        EllipseGeometry eGeometry2 = new EllipseGeometry(new Point(0, len1 - Radius), Radius, Radius);
                        RectangleGeometry rGeometry2 = new RectangleGeometry(new Rect(0, len1 - Radius, Radius, Radius));
                        CombinedGeometry comb2 = new CombinedGeometry(eGeometry2, rGeometry2);
                        comb2.GeometryCombineMode = GeometryCombineMode.Intersect;

                        drawingContext.PushClip(comb2);
                        drawingContext.DrawEllipse(radBrush, null, new Point(0, len1 - Radius), Radius, Radius);
                        drawingContext.Pop();

                        // 蚂蚁线坐标点
                        p0 = new Point(Radius / 2, 0);
                        p1 = new Point(Radius / 2, len1 - Radius);
                        p2 = new Point(Radius / 2, len1 - Radius / 2);
                        p3 = new Point(Radius, len1 - Radius / 2);

                        pathFigure.StartPoint = p0;
                        LineSegment line = new LineSegment(p1, true);
                        BezierSegment bezier = new BezierSegment(p1, p2, p3, true);

                        pathFigure.Segments.Add(line);
                        pathFigure.Segments.Add(bezier);
                    }
                    else
                    {
                        EllipseGeometry eGeometry2 = new EllipseGeometry(new Point(Radius, len1 - Radius), Radius, Radius);
                        RectangleGeometry rGeometry2 = new RectangleGeometry(new Rect(0, len1 - Radius, Radius, Radius));
                        CombinedGeometry comb2 = new CombinedGeometry(eGeometry2, rGeometry2);
                        comb2.GeometryCombineMode = GeometryCombineMode.Intersect;

                        drawingContext.PushClip(comb2);
                        drawingContext.DrawEllipse(radBrush, null, new Point(Radius, len1 - Radius), Radius, Radius);
                        drawingContext.Pop();

                        // 蚂蚁线坐标点
                        p0 = new Point(Radius / 2, 0);
                        p1 = new Point(Radius / 2, len1 - Radius);
                        p2 = new Point(Radius / 2, len1 - Radius / 2);
                        p3 = new Point(0, len1 - Radius / 2);

                        pathFigure.StartPoint = p0;
                        LineSegment line = new LineSegment(p1, true);
                        BezierSegment bezier = new BezierSegment(p1, p2, p3, true);

                        pathFigure.Segments.Add(line);
                        pathFigure.Segments.Add(bezier);
                    }
                }
                // 起点带方向，结束端点垂直
                else if (string.IsNullOrEmpty(EndDir))
                {
                    Point p0;
                    Point p1;
                    Point p2;
                    Point p3;

                    // 绘制一区弧形
                    if (BeginDir == "Left")
                    {
                        EllipseGeometry eGeometry = new EllipseGeometry(new Point(0, Radius), Radius, Radius);
                        RectangleGeometry rGeometry = new RectangleGeometry(new Rect(x: 0, 0, Radius, Radius));
                        CombinedGeometry comb = new CombinedGeometry(eGeometry, rGeometry);
                        comb.GeometryCombineMode = GeometryCombineMode.Intersect;

                        drawingContext.PushClip(comb);
                        drawingContext.DrawEllipse(radBrush, null, new Point(0, Radius), Radius, Radius);
                        drawingContext.Pop();

                        // 蚂蚁线坐标点
                        p0 = new Point(0, Radius / 2);
                        p1 = new Point(Radius / 2, Radius / 2);
                        p2 = new Point(Radius / 2, Radius);
                        p3 = new Point(Radius / 2, len1);

                        pathFigure.StartPoint = p0;
                        LineSegment line = new LineSegment(p1, true);
                        BezierSegment bezier = new BezierSegment(p1, p2, p3, true);

                        pathFigure.Segments.Add(line);
                        pathFigure.Segments.Add(bezier);
                    }
                    else
                    {
                        EllipseGeometry eGeometry = new EllipseGeometry(new Point(Radius, Radius), Radius, Radius);
                        RectangleGeometry rGeometry = new RectangleGeometry(new Rect(x: 0, 0, Radius, Radius));
                        CombinedGeometry comb = new CombinedGeometry(eGeometry, rGeometry);
                        comb.GeometryCombineMode = GeometryCombineMode.Intersect;

                        drawingContext.PushClip(comb);
                        drawingContext.DrawEllipse(radBrush, null, new Point(Radius, Radius), Radius, Radius);
                        drawingContext.Pop();

                        // 蚂蚁线坐标点
                        p0 = new Point(Radius, Radius / 2);
                        p1 = new Point(Radius / 2, Radius / 2);
                        p2 = new Point(Radius / 2, Radius);
                        p3 = new Point(Radius / 2, len1);

                        pathFigure.StartPoint = p0;
                        LineSegment line = new LineSegment(p1, true);
                        BezierSegment bezier = new BezierSegment(p1, p2, p3, true);

                        pathFigure.Segments.Add(line);
                        pathFigure.Segments.Add(bezier);
                    }
                }
                else
                {
                    // 蚂蚁线坐标点
                    Point p0;
                    Point p1;
                    Point p2;
                    Point p3;
                    Point p4;
                    Point p5;

                    // 绘制一区弧形
                    if (BeginDir == "Left")
                    {
                        EllipseGeometry eGeometry = new EllipseGeometry(new Point(0, Radius), Radius, Radius);
                        RectangleGeometry rGeometry = new RectangleGeometry(new Rect(x: 0, 0, Radius, Radius));
                        CombinedGeometry comb = new CombinedGeometry(eGeometry, rGeometry);
                        comb.GeometryCombineMode = GeometryCombineMode.Intersect;

                        drawingContext.PushClip(comb);
                        drawingContext.DrawEllipse(radBrush, null, new Point(0, Radius), Radius, Radius);
                        drawingContext.Pop();

                        p0 = new Point(0, Radius / 2);
                        p1 = new Point(Radius / 2, Radius / 2);
                        p2 = new Point(Radius / 2, Radius);
                    }
                    else
                    {
                        EllipseGeometry eGeometry = new EllipseGeometry(new Point(Radius, Radius), Radius, Radius);
                        RectangleGeometry rGeometry = new RectangleGeometry(new Rect(x: 0, 0, Radius, Radius));
                        CombinedGeometry comb = new CombinedGeometry(eGeometry, rGeometry);
                        comb.GeometryCombineMode = GeometryCombineMode.Intersect;

                        drawingContext.PushClip(comb);
                        drawingContext.DrawEllipse(radBrush, null, new Point(Radius, Radius), Radius, Radius);
                        drawingContext.Pop();

                        p0 = new Point(Radius / 2, 0);
                        p1 = new Point(Radius / 2, Radius / 2);
                        p2 = new Point(Radius / 2, Radius);
                    } 

                    // 绘制中间矩形
                    drawingContext.DrawRectangle(linearBrush, null, new Rect(0, Radius, Radius, len1 - 2 * Radius));

                    // 绘制二区弧形
                    if (EndDir == "Left")
                    {
                        EllipseGeometry eGeometry2 = new EllipseGeometry(new Point(0, len1 - Radius), Radius, Radius);
                        RectangleGeometry rGeometry2 = new RectangleGeometry(new Rect (0, len1 - Radius, Radius, Radius));
                        CombinedGeometry comb2 = new CombinedGeometry(eGeometry2, rGeometry2);
                        comb2.GeometryCombineMode = GeometryCombineMode.Intersect;

                        drawingContext.PushClip(comb2);
                        drawingContext.DrawEllipse(radBrush, null, new Point(0, len1 - Radius), Radius, Radius);
                        drawingContext.Pop();

                        // 蚂蚁线坐标点
                        p3 = new Point(Radius / 2, len1 - Radius);
                        p4 = new Point(Radius / 2, len1 - Radius / 2);
                        p5 = new Point(0, len1 - Radius / 2);
                    }
                    else
                    {
                        EllipseGeometry eGeometry2 = new EllipseGeometry(new Point(Radius, len1 - Radius), Radius, Radius);
                        RectangleGeometry rGeometry2 = new RectangleGeometry(new Rect(0, len1 - Radius, Radius, Radius));
                        CombinedGeometry comb2 = new CombinedGeometry(eGeometry2, rGeometry2);
                        comb2.GeometryCombineMode = GeometryCombineMode.Intersect;

                        drawingContext.PushClip(comb2);
                        drawingContext.DrawEllipse(radBrush, null, new Point(Radius, len1 - Radius), Radius, Radius);
                        drawingContext.Pop();

                        // 蚂蚁线坐标点
                        p3 = new Point(Radius / 2, len1 - Radius);
                        p4 = new Point(Radius / 2, len1 - Radius / 2);
                        p5 = new Point(Radius, len1 - Radius / 2);
                    }

                    pathFigure.StartPoint = p0;
                    BezierSegment bezier1 = new BezierSegment(p0, p1, p2, true);
                    LineSegment line = new LineSegment(p3, true);
                    BezierSegment bezier2 = new BezierSegment(p3, p4, p5, true);

                    pathFigure.Segments.Add(bezier1);
                    pathFigure.Segments.Add(line);
                    pathFigure.Segments.Add(bezier2);
                }
            }

            PathGeometry pathGeometry = new PathGeometry();
            pathGeometry.Figures.Add(pathFigure);

            Pen pen = new Pen();
            pen.Brush = new SolidColorBrush(Colors.Orange);
            pen.Thickness = 4;
            DashStyle style = new DashStyle();
            style.Dashes.Add(3);
            style.Dashes.Add(3);
            style.Offset = 0;
            pen.DashStyle = style;
            pen.DashCap = PenLineCap.Round;

            DoubleAnimation animation = new DoubleAnimation();
            animation.Duration = new Duration(TimeSpan.FromSeconds(0.4));
            animation.RepeatBehavior = RepeatBehavior.Forever;
            animation.From = FlowDir == FlowDirections.FORWARD ? 0 : -6;
            animation.To = FlowDir == FlowDirections.FORWARD ? -6 : 0;
            if(Flow)
                style.BeginAnimation(DashStyle.OffsetProperty, animation);

            drawingContext.DrawGeometry(null, pen, pathGeometry);
        }
    }

    public enum FlowDirections
    {
        FORWARD,
        BACK
    }
}
